package com.data.modules.smzh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.modules.smzh.entity.Register;
import com.data.modules.smzh.service.IRegisterService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: register
 * @Author: data-boot
 * @Date:   2023-01-17
 * @Version: V1.0
 */
@RestController
@RequestMapping("/smzh/register")
@Slf4j
public class RegisterAppController extends BootController<Register, IRegisterService> {
	@Autowired
	private IRegisterService registerService;

	/**
	 * 分页列表查询
	 *
	 * @param register
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="register-分页列表查询", notes="register-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Register register,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Register> queryWrapper = QueryGenerator.initQueryWrapper(register, req.getParameterMap());
		Page<Register> page = new Page<Register>(pageNo, pageSize);
		IPage<Register> pageList = registerService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param register
	 * @return
	 */
	@ApiOperation(value="register-添加", notes="register-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Register register) {
		registerService.save(register);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param register
	 * @return
	 */
	@ApiOperation(value="register-编辑", notes="register-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Register register) {
		registerService.updateById(register);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="register-通过id删除", notes="register-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		registerService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="register-批量删除", notes="register-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.registerService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="register-通过id查询", notes="register-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Register register = registerService.getById(id);
		if(register==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(register);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param register
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, Register register) {
		return super.exportXls(request, register, Register.class, "register");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, Register.class);
	}

}
